package com.deli.pagingforjava.adapter;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.paging.PagingDataAdapter;
import androidx.recyclerview.widget.DiffUtil;
import androidx.recyclerview.widget.RecyclerView;

import com.deli.pagingforjava.PagingHelper;
import com.deli.pagingforjava.R;
import com.deli.pagingforjava.bean.BaseItemBean;
import com.deli.pagingforjava.bean.NoMoreDataBean;

import org.jetbrains.annotations.NotNull;

import java.lang.reflect.Constructor;

/**
 * @Author: QDL
 * @CreateDate: 2021/4/23 10:20
 */
public abstract class BasePagingAdapter<T extends BaseItemBean, VH extends RecyclerView.ViewHolder> extends PagingDataAdapter<T, RecyclerView.ViewHolder> {
    private static final String TAG = "PagingForJava";
    Context context;
    LayoutInflater layoutInflater;
    BaseFooterAdapter.OnFooterViewListener listener;

    public BasePagingAdapter(Context context, @NotNull DiffUtil.ItemCallback<T> diffCallback) {
        super(diffCallback);
        this.context = context;
        layoutInflater = LayoutInflater.from(context);
    }

    /**
     * 绑定RecyclerView 且优先使用全局指定的加载更多的Adapter,没有的情况使用默认
     *
     * @param rvList
     */
    public void bindToRecyclerView(RecyclerView rvList) {
        BaseFooterAdapter<?> footerAdapter = null;
        try {
            Constructor<?> constructor = PagingHelper.getDefault().getFooterAdapterClass().getConstructor(LayoutInflater.class);
            footerAdapter = (BaseFooterAdapter<?>) constructor.newInstance(layoutInflater);
        } catch (Exception e) {
            e.printStackTrace();
            Log.e(TAG, "bindToRecyclerView: ", new Exception("FooterAdapter必须包含一个只有一个参数LayoutInflater的构造器，最好也只有这一个构造器"));
        }
        if (footerAdapter != null) {
            bindToRecyclerView(rvList, footerAdapter);
            return;
        }

        rvList.setAdapter(withLoadStateFooter(new SimpleFooterAdapter(layoutInflater).setOnFooterViewListener(this::retry)));
    }


    /**
     * 绑定RecyclerView ,指定是否显示加载更多
     *
     * @param rvList
     * @param canLoadMore
     */
    public void bindToRecyclerView(RecyclerView rvList, boolean canLoadMore) {
        if (canLoadMore) {
            bindToRecyclerView(rvList);
        } else {
            rvList.setAdapter(this);
        }
    }

    /**
     * 绑定RecyclerView 且指定加载Adapter
     *
     * @param rvList
     * @param footerAdapter
     */
    public void bindToRecyclerView(RecyclerView rvList, BaseFooterAdapter footerAdapter) {
        footerAdapter.setOnFooterViewListener(() -> {
            if (listener != null) {
                listener.onRetry();
            }
        });
        rvList.setAdapter(withLoadStateFooter(footerAdapter));
    }

    /**
     * 设置尾部监听
     *
     * @param listener
     */
    public void setOnFooterViewListener(BaseFooterAdapter.OnFooterViewListener listener) {
        this.listener = listener;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        if (viewType == NoMoreDataViewHolder.TYPE_ITEM) {
            return new NoMoreDataViewHolder(layoutInflater.inflate(R.layout.no_more_data_layout, parent, false));
        }
        return onPagingCreateViewHolder(parent, viewType);
    }

    /**
     * 同onCreateViewHolder
     *
     * @param parent
     * @param viewType
     * @return
     */
    @NonNull
    public abstract VH onPagingCreateViewHolder(@NonNull ViewGroup parent, int viewType);

    @Override
    public int getItemViewType(int position) {
        if (getItem(position) instanceof NoMoreDataBean) {
            return NoMoreDataViewHolder.TYPE_ITEM;
        }
        return getPagingItemViewType(position);
    }

    /**
     * 同getItemViewType
     *
     * @param position
     * @return
     */
    public abstract int getPagingItemViewType(int position);

    protected static class NoMoreDataViewHolder extends RecyclerView.ViewHolder {
        static final int TYPE_ITEM = -999;

        NoMoreDataViewHolder(@NonNull View itemView) {
            super(itemView);
        }
    }
}
